package com.example.cv;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyContentProvider extends ContentProvider {
      SQLiteDatabase db;
      //定义匹配码
    private final static UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
        private static final int QUERY_MATCHED = 1;
        private static final int INSERT_MATCHED = 2;
        private static final int UPDATE_MATCHED = 3;
        private static final int DELETE_MATCHED = 4;

    //给匹配器添加 匹配规则(添加新的uri路径)
    static {

            MATCHER.addURI("com.example.studentProvide", "query", QUERY_MATCHED);
            MATCHER.addURI("com.example.studentProvide", "insert", INSERT_MATCHED);
            MATCHER.addURI("com.example.studentProvide", "update", UPDATE_MATCHED);
            MATCHER.addURI("com.example.studentProvide", "delete", DELETE_MATCHED);

        }
        @Override
        public boolean onCreate() {
            // 当内容提供者创建的时候 会走这个方法
            //在这里可以创建openHelper但是不要打开数据库
            MyOpenHelper openHelper = new MyOpenHelper(getContext());
            db = openHelper.getWritableDatabase();
            return false;
        }

        @Nullable
        @Override
        public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection,
                            @Nullable String[] selectionArgs, @Nullable String sortOrder) {
            int result = MATCHER.match(uri);
            if(result == QUERY_MATCHED){

                Cursor cursor = db.query("info", projection, selection, selectionArgs, null, null, sortOrder);
                return cursor;
            }else{
                throw new IllegalArgumentException("匹配错误");
            }
        }

        @Nullable
        @Override
        public String getType(@NonNull Uri uri) {
            return null;
        }

        @Nullable
        @Override
        public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
            //先匹配传入的uri
            int result = MATCHER.match(uri);
            if(result==INSERT_MATCHED){
                //如果匹配的结果是 insert的话 执行insert的操作 返回结果
                long info = db.insert("info", null, values);
                db.close();
                //当插入一条数据的时候 数据库的内容发生了变化 可以通过内容解析者发出一个通知
                ContentResolver resolver = getContext().getContentResolver();
                //第一个参数 uri 通过这个uri来确定 我要通知那些内容观察者
                //第二个参数  如果传入了一个内容观察者 只有这个内容观察者能够收到消息 可以传null 如果是null
                //只要注册了相同的uri就可以收到数据变化的消息
                resolver.notifyChange(uri,null);
                return Uri.parse(String.valueOf(info));
            }else{
                //结果没有匹配 返回null;
                return null;
            }
        }
        @Override
        public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
            int match =MATCHER.match(uri);
            if(DELETE_MATCHED == match){

                int result = db.delete("info", selection, selectionArgs);
                db.close();
                return result;
            }else{
                return -1;
            }
        }

        @Override
        public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
            if(UPDATE_MATCHED == MATCHER.match(uri)){

                int result = db.update("info", values, selection, selectionArgs);
                db.close();
                return result;
            }else{
                return -1;
            }
        }
    }